package com.kasinf.springcloud.alibaba.service.impl;

import com.kasinf.springcloud.alibaba.dao.OrderDao;
import com.kasinf.springcloud.alibaba.domain.Order;
import com.kasinf.springcloud.alibaba.service.AccountService;
import com.kasinf.springcloud.alibaba.service.OrderService;
import com.kasinf.springcloud.alibaba.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;

    @Resource
    private StorageService storageService;

    @Resource
    private AccountService accountService;

    @Override
    @GlobalTransactional(name = "fsp-create-order", rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("------> 开始创建订单");
        orderDao.create(order);

        log.info("------> 订单微服务开始调用库存服务");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("------> 库存服务扣减完成");

        log.info("------> 订单微服务开始调用账户服务");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("------> 账户服务扣减完成");

        log.info("------> 开始修改订单状态");
        orderDao.update(order.getUserId(), 0);
        log.info("------> 修改订单状态结束");

        log.info("------> 下单成功！！！！");
    }
}
